namespace Universal.Domain.Entities.App;

public class AppRole : EntityBase
{
    public string Rolename { get; set; } = string.Empty;
    //角色描述
    public string? Description { get; set; }
    public bool IsDefault { get; set; } = false;

    //一个角色拥有多个用户
    public ICollection<AppUser> Users { get; set; } = new List<AppUser>();
    public ICollection<AppPermission> Permissions { get; set; } = new List<AppPermission>();
    public ICollection<AppMenu> Menus { get; set; } = new List<AppMenu>();

    /// <summary>
    /// 分配权限
    /// </summary>
    /// <param name="permission"></param>
    /// <returns></returns>
    public bool AssignPermission(AppPermission permission)
    {
        if (!Permissions.Contains(permission))
        {
            Permissions.Add(permission);
            return true;
        }
        return false;
    }

    /// <summary>
    /// 分配菜单
    /// </summary>
    /// <param name="menu"></param>
    /// <returns></returns>
    /// <summary>
    /// 分配菜单（覆盖式分配）
    /// </summary>
    public void AssignMenus(IEnumerable<AppMenu> menus)
    {
        Menus.Clear();
        foreach (var menu in menus)
        {
            Menus.Add(menu);
        }
    }

    /// <summary>
    /// 移除权限
    /// </summary>
    /// <param name="permission"></param>
    /// <returns></returns>
    public bool RemovePermission(AppPermission permission)
    {
        if (Permissions.Contains(permission))
        {
            Permissions.Remove(permission);
            return true;
        }
        return false;
    }

}